package com.levi.utils.data;

import android.content.Context;
import android.content.SharedPreferences;

import com.levi.utils.JsonUtils;

import java.lang.reflect.Type;

import androidx.annotation.NonNull;


/**
 * Created by Levi on 2018/3/13.
 */

public class SharePref {
    private SharedPreferences mSharePref;

    public SharePref(@NonNull Context context, @NonNull String prefName) {
        mSharePref = context.getSharedPreferences(prefName, Context.MODE_PRIVATE);
    }

    public void save(String key, String value) {
        SharedPreferences.Editor editor = editor();
        editor.putString(key, value);
        editor.apply();
    }

    public String get(String key, String def) {
        return mSharePref.getString(key, def);
    }

    public void save(String key, boolean value) {
        SharedPreferences.Editor editor = editor();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public boolean get(String key, boolean def) {
        return mSharePref.getBoolean(key, def);
    }

    public void save(String key, long value) {
        SharedPreferences.Editor editor = editor();
        editor.putLong(key, value);
        editor.apply();
    }

    public long get(String key, long def) {
        return mSharePref.getLong(key, def);
    }

    public void save(String key, int value) {
        SharedPreferences.Editor editor = editor();
        editor.putInt(key, value);
        editor.apply();
    }

    public double get(String key, double def) {
        String value = mSharePref.getString(key, null);
        if (value == null) {
            return def;
        } else {
            return Double.parseDouble(value);
        }
    }

    public void save(String key, double value) {
        SharedPreferences.Editor editor = editor();
        editor.putString(key, String.valueOf(value));
        editor.apply();
    }

    public int get(String key, int def) {
        return mSharePref.getInt(key, def);
    }

    public void save(String key, float value) {
        SharedPreferences.Editor editor = editor();
        editor.putFloat(key, value);
        editor.apply();
    }

    public float get(String key, float def) {
        return mSharePref.getFloat(key, def);
    }

    public void saveObject(String key, Object obj) {
        String value = JsonUtils.toJson(obj);
        SharedPreferences.Editor editor = editor();
        editor.putString(key, value);
        editor.apply();
    }

    public <T> T getObject(String key, Type type) {
        String json = get(key, null);
        return JsonUtils.fromJson(json, type);
    }

    public SharedPreferences.Editor editor() {
        return mSharePref.edit();
    }

    public void clear() {
        SharedPreferences.Editor editor = editor();
        editor.clear();
        editor.apply();
    }

    public void remove(String key) {
        SharedPreferences.Editor editor = editor();
        editor.remove(key);
        editor.apply();
    }

    public void remove(String... keys) {
        SharedPreferences.Editor editor = editor();
        for (String key : keys) {
            editor.remove(key);
        }
        editor.apply();
    }
}
